from django.shortcuts import render
from rest_framework .views import APIView
from rest_framework.response import Response
from django.contrib.auth.hashers import make_password,check_password
from utils.comm import *
from .ser import *
import random

class Deng(APIView):
    def get(self,request):
        #获取手机验证码
        mobile = request.GET.get('mobile')
        yzm = random.randint(100000,999999)
        me = send_message(mobile,yzm,1)
        #保存验证码
        conn = get_redis_conn()
        conn.setex('123123',10,str(yzm))
        import redis
        conn = redis.Redis(host='localhost',port=6379)
        conn.setex('123123',1000,str(yzm))
        if me:
            return Response({'code':200})
        else:
            return Response({"code":10010})
    def post(self,request):
        request.data['password']=make_password(request.data.get('password'))
        conn = get_redis_conn()
        print(conn.get('123123'))
        print(request.data)
        code = str(conn.get('123123'),encoding='utf8')
        if request.data.get('yzm') == conn:
            ser =LoginSer(data=request.data)
            if ser.is_valid():
                ser.save()
                return Response({'code':200})
            else:
                return Response({'code':10010})
        else:
            return Response({'code':10011})

# Create your views here.
